package xm.chengdu.com.beijing_211.Thread;

import android.util.Log;

import utils.HexUtil;
import utils.WW;

/**
 * Created by Administrator on 2018/11/30.
 */

public class YanZheng {
    //当前步骤
    private int g_nStep55AA = 0;
    //当前数据是第几字节
    public int g_nLenFrame55AA = 0;
    private int g_nLenData = 0;
    //当前字节数
    private int g_nPointData = 0;
    public int g_nCmd = 0;
    //收到的byte数组的缓存数组
    public byte[] g_bytesRecv = new byte[512];
    //第一步55
    private final int STEP_55 = 0;
    //第二步AA
    private final int STEP_AA = 1;
    //第三步 命令字
    private final int STEP_CMD = 2;
   //第四步 长度
    private final int STEP_LEN = 3;
     //第五步 数据区
    private final int STEP_DATA = 4;
    //第六步 校验
    private final int STEP_VERIFY = 7;
    //第七步 结束
    private final int STEP_END = 8;
    public int Analy55AA(byte[] data,int nLen)
    {
        int nLenFrame= nLen;

        for(int i=0;i<nLenFrame;i++)
        {
            switch(g_nStep55AA)
            {
                case STEP_55://55
                    if( (int)(data[i]&0xFF) == 0x55 )
                    {
                        g_nStep55AA = STEP_AA;
                        g_nLenFrame55AA = 0;
                        g_nPointData = 0;
                        g_nLenData = 0;
                        g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    }else {
                        g_nStep55AA = STEP_END;
                    }
                    break;
                case STEP_AA:
                    if( (int)(data[i]&0xFF) == 0xAA )
                    {
                        g_nStep55AA = STEP_CMD;
                        g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    }
                    else
                        g_nStep55AA = STEP_END;
                    break;
                case STEP_CMD:
                    g_nCmd = (int)(data[i]&0xFF);
                    g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    g_nStep55AA = STEP_LEN;
                    break;
                case STEP_LEN:
                    g_nLenData = (int)(data[i]&0xFF);
                    g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    g_nStep55AA = STEP_DATA;

                    break;
                case STEP_DATA:
                    g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    g_nPointData++;
                    if( g_nPointData >= g_nLenData )
                        g_nStep55AA = STEP_VERIFY;
                    break;
                case STEP_VERIFY:
                   byte byteVerify = 0;
                    for( int k = 0; k < g_nLenFrame55AA-2; k++ )
                    {
                        byteVerify ^= g_bytesRecv[k];
                    }
                    g_bytesRecv[g_nLenFrame55AA++] = data[i];
                    String str3=   HexUtil.BytesToString(g_bytesRecv,0,g_nLenFrame55AA);
                    String substring1 = str3.substring(0, str3.length() - 2);
                    String jiegguo = WW.makeChecksum(substring1);
                    //计算的值
                    String substrings = jiegguo.substring(jiegguo.length() - 2, jiegguo.length());
                    String shoudaode = str3.substring(str3.length() - 2, str3.length());
                    Integer xxx = Integer.parseInt(substrings, 16);
                    Integer xxxs = Integer.parseInt(shoudaode, 16);
                    if(	xxx.equals(xxxs) )
                    {
                        Log.e("test","计算校验正确");
                        g_nStep55AA = STEP_55;
                        return 0;
                    }else
                    {
                        g_nStep55AA = STEP_55;
                        Log.e("test","计算校验出错"+ HexUtil.BytesToString(g_bytesRecv,0,g_nLenFrame55AA));
                        return 2;//校验出错
                    }
                    //break;
                default:
                    g_nStep55AA = STEP_55;
                    return 3;
            }
        }
        return 1;
    }


    public byte[] GetFrameData55AA()
    {
        byte[] bytesTmp = new byte[g_nLenFrame55AA];
        System.arraycopy(g_bytesRecv, 0, bytesTmp, 0,g_nLenFrame55AA);
        return bytesTmp;
    }

}
